﻿using GPSTrackingWebsiteManager.ClassManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class StockFileSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(List<GridDataItem> rows);
        public event AcceptEvent OnAcceptEvent;

        public delegate void CancelEvent();
        public event CancelEvent OnCancelEvent;

        //Propeties
        [Browsable(true), Category("Custom")]
        public bool IsMultipleSelects { get; set; }

        [Browsable(true), Category("Custom")]
        public bool IsClearGridAfterClose { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            StockFileSearchGrid.AllowMultiRowSelection = IsMultipleSelects;
            sdsFileType.ConnectionString = GPSTrackingDBV2.Data.DataRepository.ConnectionStrings["netTiersConnectionString"].ConnectionString; 

            if (!IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.StockFilesColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.StockFileSearchGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                if (IsClearGridAfterClose)
                    ClearStockFileGrid();
            }
        }

        protected void btnStockFileOK_Click(object sender, EventArgs e)
        {
            List<GridDataItem> rows = new List<GridDataItem>();
            foreach (Telerik.Web.UI.GridDataItem selectedItem in StockFileSearchGrid.SelectedItems)
            {
                rows.Add(selectedItem);
            }

            if (IsClearGridAfterClose)
                ClearStockFileGrid();

            OnAcceptEvent(rows);
        }

        protected void btnStockFileCancel_Click(object sender, EventArgs e)
        {
            if (IsClearGridAfterClose)
                ClearStockFileGrid();
            OnCancelEvent();
        }

        public void ClearStockFileGrid()
        {
            StockFileSearchGrid.DataSource = new string[] { };
            StockFileSearchGrid.DataBind();
        }

        protected void btnStockFileSearch_Click(object sender, EventArgs e)
        {
            SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];            
            string Username =  Convert.ToString(objSessionManager.Items["Username"]);
             
            StockFileSearchGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.StockFilesProvider.GetOnlyFilesWithinByUsername(objSessionManager.CompanyIDSelectedComboBox, Username, txtName.Text, txtFileName.Text);
            StockFileSearchGrid.DataBind();
        }        
    }
}